package com.wandian.cqcsk.mvvm.mine.setnewstable;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.widget.DragRecycleView;
import com.wandian.cqcsk.R;
import com.wandian.cqcsk.adapter.mine.HomeBtnDragRecycleAdapter;
import com.wandian.cqcsk.adapter.mine.MineSetNewsTableRecycleAdapter;
import com.wandian.cqcsk.databinding.ActivitySetNewsTableBinding;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description SetNewsTableActivity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_SET_NEWS_TABLE)
public class SetNewsTableActivity extends BaseMVVMActivity<SetNewsTableViewModel> {
    private ActivitySetNewsTableBinding dataBinding;
    private MineSetNewsTableRecycleAdapter mineSetNewsTableRecycleAdapter;
    private HomeBtnDragRecycleAdapter adapter;
    private DragRecycleView dragRecycleView;
    private boolean isEdit = false;
    private ArrayList<String> lisTitleSelect = new ArrayList<>();
    private String fixedString = "国内新闻";//固定不可去掉
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_set_news_table);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();
        initTopRecycle();
        initSortRecycle();
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("新闻栏目");
        dataBinding.bindTransparentToolBar.toolBarTitleRight.setVisibility(View.VISIBLE);
        dataBinding.bindTransparentToolBar.toolBarTitleRight.setText("编辑");
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        dataBinding.bindTransparentToolBar.toolBarTitleRight.setOnClickListener(v -> {
            if(dataBinding.bindTransparentToolBar.toolBarTitleRight.getText().toString().equals("保存并更新")){
                AppContext.getInstance().getAppPreferencesHelper().setKeyValue("home_news_json", JsonUtil.toJson(lisTitleSelect));
                ARouter.getInstance().build(RouterConfig.ROUTER_MAIN).navigation();
            }else {
                isEdit = !isEdit;
                if(isEdit){
                    dataBinding.bindTransparentToolBar.toolBarTitleRight.setText("保存并更新");
                }else {
                    dataBinding.bindTransparentToolBar.toolBarTitleRight.setText("编辑");
                }
                mineSetNewsTableRecycleAdapter.notifySelectState(isEdit,lisTitleSelect);
            }

        });
    }

    private void initTopRecycle(){
        lisTitleSelect.clear();
        String tableJson = AppContext.getInstance().getAppPreferencesHelper().getKeyValue("home_news_json");
        if(UIUtils.isEmpty(tableJson)){//默认底部选项
            lisTitleSelect.add("国内新闻");
            lisTitleSelect.add("头条新闻");
            lisTitleSelect.add("国际新闻");
            lisTitleSelect.add("财经新闻");
            lisTitleSelect.add("汽车新闻");
            lisTitleSelect.add("体育新闻");
            lisTitleSelect.add("娱乐新闻");
            lisTitleSelect.add("科技新闻");
            lisTitleSelect.add("军事新闻");
            AppContext.getInstance().getAppPreferencesHelper().setKeyValue("home_news_json", JsonUtil.toJson(lisTitleSelect));
        }else {
            try {
                JSONArray jsonArray = new JSONArray(tableJson);
                if(!UIUtils.isEmpty(jsonArray) && jsonArray.length()>0){
                    for(int i=0;i<jsonArray.length();i++){
                        lisTitleSelect.add(jsonArray.getString(i));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        adapter = new HomeBtnDragRecycleAdapter(R.layout.common_item_recycle_text_btn,lisTitleSelect);
        dragRecycleView = new DragRecycleView();
        dragRecycleView.initCustomDragRecycler(dataBinding.recycler,adapter,3,fixedString);
        dragRecycleView.notifyDragRecycleViewData(lisTitleSelect);
        adapter.setNewData(lisTitleSelect);
    }

    private void initSortRecycle(){
        mineSetNewsTableRecycleAdapter = new MineSetNewsTableRecycleAdapter(R.layout.item_recycle_text_btn_edit,lisTitleSelect);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,3);
        dataBinding.recyclerSort.setLayoutManager(layoutManager);
        dataBinding.recyclerSort.setAdapter(mineSetNewsTableRecycleAdapter);
        List<String> lisTitle = new ArrayList<>();
        lisTitle.add("国内新闻");
        lisTitle.add("国际新闻");
        lisTitle.add("财经新闻");
        lisTitle.add("汽车新闻");
        lisTitle.add("体育新闻");
        lisTitle.add("娱乐新闻");
        lisTitle.add("健康知识");
        lisTitle.add("IT新闻");
        lisTitle.add("科技新闻");
        lisTitle.add("军事新闻");
        lisTitle.add("头条新闻");
        lisTitle.add("科学探索");
        lisTitle.add("环保资讯");
        lisTitle.add("币圈资讯");
        lisTitle.add("房产新闻");
        lisTitle.add("地区新闻");
        lisTitle.add("综合新闻");
        lisTitle.add("农业新闻");
        lisTitle.add("动漫资讯");
        lisTitle.add("CBA新闻");
        lisTitle.add("人工智能");
        lisTitle.add("区块链新闻");
        lisTitle.add("旅游资讯");
        lisTitle.add("移动通信");
        lisTitle.add("苹果新闻");
        lisTitle.add("创业新闻");
        lisTitle.add("NBA新闻");
        lisTitle.add("社会新闻");

        mineSetNewsTableRecycleAdapter.setNewData(lisTitle);
        mineSetNewsTableRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                if(isEdit){
                    lisTitleSelect = dragRecycleView.getLisTitle();
                    String itemString = baseQuickAdapter.getData().get(position).toString();
                    if(!itemString.equals(fixedString)){
                        boolean isAdd = true;
                        int index = 0;
                        for(int i=0;i<lisTitleSelect.size();i++){
                            if(itemString.equals(lisTitleSelect.get(i))){
                                isAdd = false;
                                index = i;
                                break;
                            }
                        }
                        if(isAdd){
                            if(lisTitleSelect.size()>=9){
                                ToastUtil.show("亲，关注的新闻栏目不能在加了~");
                            }else {
                                lisTitleSelect.add(itemString);
                                notifyUIAdapter();
                            }
                        }else {
                            lisTitleSelect.remove(index);
                            notifyUIAdapter();
                        }

                    }else {
                        ToastUtil.show("该项不可编辑");
                    }

                }

            }
        });
    }

    private void notifyUIAdapter(){
        dragRecycleView.notifyDragRecycleViewData(lisTitleSelect);
        adapter.setNewData(lisTitleSelect);
        adapter.notifyDataSetChanged();
        mineSetNewsTableRecycleAdapter.notifySelectState(isEdit,lisTitleSelect);
    }
}
